import Vue from 'vue'

const homework = {
  state: () => ({
    currentHomework: {
      record: {},
      questions: [],
      questionIndex: -1
    }
  }),
  mutations: {
    setCurrentHomework(state, payload) {
      state.currentHomework = payload
    },
    toPrevQuestion(state) {
      if (state.currentHomework.questionIndex >= 1) {
        state.currentHomework.questionIndex--
      } else {
        Vue.prototype.$message.info('无更多题目')
      }
    },
    toNextQuestion(state) {
      if (state.currentHomework.questionIndex < state.currentHomework.questions.length - 1) {
        state.currentHomework.questionIndex++
      } else {
        Vue.prototype.$message.info('无更多题目')
      }
    }
  },
  actions: {
    initHomeworkStorage({ getters }) {
      const homeworkStorage = Vue.prototype.$storage.homework
      homeworkStorage.init(getters.studentId)
    },
    initHomeworkInputStorage({ getters }) {
      const homeworkInputStorage = Vue.prototype.$storage.homework_input
      homeworkInputStorage.init(getters.studentId)
    }
  }
}

export default homework
